package bb.lanxing.view.sport;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.view.MotionEventCompat;
import androidx.core.view.ViewCompat;
import androidx.core.widget.TextViewCompat;
import androidx.customview.widget.ViewDragHelper;

import bb.lanxing.R;
import bb.lanxing.lib.widget.utils.Density;

public class DragStopView extends FrameLayout {
    public static final int STATE_DRAGGING = 1;
    public static final int STATE_END = 3;
    public static final int STATE_SETTLING = 2;
    public static final int STATE_START = 0;
    private boolean canDrag;
    private int mDragx;
    private int mState;
    private int mTouchSlop;
    private ViewDragHelper mViewDragHelper;
    private Rect scrollRect;
    private StateChangedCallback stateChangedCallback;
    private View thumbView;

    public interface StateChangedCallback {
        void onStateChanged(int i);
    }

    public int clamp(int i, int i2, int i3) {
        return i < i2 ? i2 : Math.min(i, i3);
    }

    public DragStopView(Context context) {
        this(context, null, 0);
    }

    public DragStopView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public DragStopView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.scrollRect = new Rect();
        this.canDrag = false;
        buildChild(context, attributeSet);
        init(context);
    }

    private void buildChild(Context context, AttributeSet attributeSet) {
        int dp2px = Density.dp2px(context, 18.0f);
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.DragStopView);
        String string = obtainStyledAttributes.getString(0);
        int color = obtainStyledAttributes.getColor(1, -1092267);
        int dimensionPixelSize = obtainStyledAttributes.getDimensionPixelSize(2, dp2px);
        Drawable drawable = obtainStyledAttributes.getDrawable(3);
        Drawable drawable2 = obtainStyledAttributes.getDrawable(4);
        obtainStyledAttributes.recycle();
        final TextView textView = new TextView(context);
        textView.setText(string);
        textView.setTextSize(0, dimensionPixelSize);
        textView.setTextColor(color);
        textView.setGravity(Gravity.CENTER);
        textView.setTypeface(Typeface.DEFAULT_BOLD);
        TextViewCompat.setAutoSizeTextTypeWithDefaults(textView, 1);
        final ImageView imageView = new ImageView(context);
        if (Build.VERSION.SDK_INT >= 16) {
            imageView.setBackground(drawable);
        } else {
            imageView.setBackgroundDrawable(drawable);
        }
        imageView.setImageDrawable(drawable2);
        imageView.setScaleType(ImageView.ScaleType.CENTER);
        this.thumbView = imageView;
        LayoutParams layoutParams = new LayoutParams(-1, -2);
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        textView.setGravity(Gravity.CENTER);
        addView(textView, layoutParams);
        LayoutParams layoutParams2 = new LayoutParams(-2, -2);
        layoutParams2.gravity = Gravity.START | Gravity.CENTER_VERTICAL;//8388627;
        addView(imageView, layoutParams2);
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                DragStopView.this.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                textView.setPadding(imageView.getWidth(), 0, 0, 0);
            }
        });
    }

    private void init(Context context) {
        this.mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        this.mViewDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View view, int i) {
                return view == DragStopView.this.thumbView;
            }

            @Override
            public int clampViewPositionHorizontal(View view, int i, int i2) {
                DragStopView dragStopView = DragStopView.this;
                return dragStopView.clamp(i, dragStopView.scrollRect.left, DragStopView.this.scrollRect.right - view.getWidth());
            }

            @Override
            public int clampViewPositionVertical(View view, int i, int i2) {
                return view.getTop();
            }

            @Override
            public int getViewHorizontalDragRange(View view) {
                return super.getViewHorizontalDragRange(view);
            }

            @Override
            public void onViewDragStateChanged(int i) {
                super.onViewDragStateChanged(i);
                if (i == 1) {
                    DragStopView.this.setStateInternal(1);
                }
            }

            @Override
            public void onViewPositionChanged(View view, int i, int i2, int i3, int i4) {
                super.onViewPositionChanged(view, i, i2, i3, i4);
                DragStopView.this.mDragx = i;
            }

            @Override
            public void onViewReleased(View view, float f, float f2) {
                int i;
                int i2;
                super.onViewReleased(view, f, f2);
                if (DragStopView.this.isCloseEnd(f)) {
                    i = 3;
                    i2 = DragStopView.this.scrollRect.right - view.getWidth();
                } else {
                    i = 0;
                    i2 = DragStopView.this.scrollRect.left;
                }
                if (DragStopView.this.mViewDragHelper.settleCapturedViewAt(i2, view.getTop())) {
                    DragStopView.this.setStateInternal(2);
                    ViewCompat.postOnAnimation(view, new SettleRunnable(view, i));
                    return;
                }
                DragStopView.this.setStateInternal(i);
            }
        });
    }

    public boolean isCloseEnd(float f) {
        float width = this.scrollRect.width() - this.thumbView.getWidth();
        return ((float) this.thumbView.getLeft()) > 0.66f * width || (Density.px2dp(getContext(), f) > 3000 && ((float) this.thumbView.getLeft()) > width * 0.5f);
    }

    public void setState(int i) {
        setState(i, true);
    }

    public void setState(final int i, boolean z) {
        if (this.mState == i) {
            return;
        }
        if (i != 0 && i != 3) {
            return;
        }
        this.mState = i;
        if (z) {
            ViewParent parent = this.thumbView.getParent();
            if (parent != null && parent.isLayoutRequested() && ViewCompat.isAttachedToWindow(this.thumbView)) {
                this.thumbView.post(new Runnable() {
                    @Override
                    public void run() {
                        DragStopView dragStopView = DragStopView.this;
                        dragStopView.startSettlingAnimation(dragStopView.thumbView, i);
                    }
                });
            } else {
                startSettlingAnimation(this.thumbView, i);
            }
        } else if (i == 0) {
            ViewCompat.offsetLeftAndRight(this.thumbView, this.scrollRect.left - this.thumbView.getLeft());
            this.mDragx = this.scrollRect.left;
        } else {
            int width = this.scrollRect.right - this.thumbView.getWidth();
            ViewCompat.offsetLeftAndRight(this.thumbView, width);
            this.mDragx = width;
        }
    }

    public int getState() {
        return this.mState;
    }

    public void startSettlingAnimation(View view, int i) {
        int width;
        if (i == 0) {
            width = this.scrollRect.left;
        } else if (i == 3) {
            width = this.scrollRect.right - view.getWidth();
        } else {
            throw new IllegalArgumentException("Illegal state argument: " + i);
        }
        setStateInternal(2);
        if (this.mViewDragHelper.smoothSlideViewTo(view, width, view.getTop())) {
            ViewCompat.postOnAnimation(view, new SettleRunnable(view, i));
        }
    }

    @Override
    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        int i5;
        super.onLayout(z, i, i2, i3, i4);
        if (z) {
            this.scrollRect.set(getPaddingLeft(), 0, (i3 - i) - getPaddingRight(), (i4 - i2) - getPaddingBottom());
        }
        View view = this.thumbView;
        if (view != null) {
            int left = view.getLeft();
            int i6 = this.mState;
            if (i6 == 0) {
                i5 = this.scrollRect.left;
            } else if (i6 == 3) {
                i5 = this.scrollRect.right - this.thumbView.getWidth();
            } else {
                i5 = this.mDragx;
            }
            int i7 = i5 - left;
            if (i7 == 0) {
                return;
            }
            ViewCompat.offsetLeftAndRight(this.thumbView, i7);
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
        if (motionEvent.getAction() == 0) {
            View findTopChildUnder = findTopChildUnder((int) motionEvent.getX(), (int) motionEvent.getY());
            this.canDrag = findTopChildUnder != null && findTopChildUnder == this.thumbView;
        }
        if (this.canDrag) {
            if (getParent() != null) {
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            this.mViewDragHelper.processTouchEvent(motionEvent);
            return true;
        }
        return this.mViewDragHelper.shouldInterceptTouchEvent(motionEvent);
    }

    public View findTopChildUnder(int i, int i2) {
        if (i < this.thumbView.getLeft() || i >= this.thumbView.getRight() || i2 < this.thumbView.getTop() || i2 >= this.thumbView.getBottom()) {
            return null;
        }
        return this.thumbView;
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int actionMasked = MotionEventCompat.getActionMasked(motionEvent);
        if (this.mState == 1 && actionMasked == 0) {
            return true;
        }
        this.mViewDragHelper.processTouchEvent(motionEvent);
        float x = motionEvent.getX() - this.thumbView.getLeft();
        float y = motionEvent.getY() - this.thumbView.getTop();
        boolean z = x >= 0.0f && x < ((float) this.thumbView.getRight()) && y > 0.0f && y < ((float) this.thumbView.getBottom());
        int actionMasked2 = motionEvent.getActionMasked();
        if (actionMasked2 == 0) {
            getParent().requestDisallowInterceptTouchEvent(true);
            if (z) {
                setPressed(this.thumbView, true, x, y);
            }
        } else if (actionMasked2 == 1) {
            getParent().requestDisallowInterceptTouchEvent(false);
            if (z) {
                setPressed(this.thumbView, true, x, y);
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        DragStopView.this.thumbView.setPressed(false);
                    }
                }, ViewConfiguration.getPressedStateDuration());
            }
        } else if (actionMasked2 == 2) {
            getParent().requestDisallowInterceptTouchEvent(true);
            if (z && Build.VERSION.SDK_INT >= 21) {
                this.thumbView.drawableHotspotChanged(x, y);
            }
            if (!pointInView(this.thumbView, x, y, this.mTouchSlop)) {
                this.thumbView.setPressed(false);
            }
        } else if (actionMasked2 == 3) {
            getParent().requestDisallowInterceptTouchEvent(true);
            if (z || this.thumbView.isPressed()) {
                this.thumbView.setPressed(false);
            }
        }
        return true;
    }

    private void setPressed(View view, boolean z, float f, float f2) {
        if (Build.VERSION.SDK_INT >= 21 && z) {
            view.drawableHotspotChanged(f, f2);
        }
        view.setPressed(z);
    }

    public boolean pointInView(View view, float f, float f2, float f3) {
        float f4 = -f3;
        return f >= f4 && f2 >= f4 && f < ((float) (view.getRight() - view.getLeft())) + f3 && f2 < ((float) (view.getBottom() - view.getTop())) + f3;
    }

    public void setStateInternal(int i) {
        if (this.mState == i) {
            return;
        }
        this.mState = i;
        StateChangedCallback stateChangedCallback = this.stateChangedCallback;
        if (stateChangedCallback == null) {
            return;
        }
        stateChangedCallback.onStateChanged(i);
    }

    public boolean canDrag() {
        return this.canDrag;
    }

    public void setCanDrag(boolean z) {
        this.canDrag = z;
    }

    public StateChangedCallback getStateChangedCallback() {
        return this.stateChangedCallback;
    }

    public void setStateChangedCallback(StateChangedCallback stateChangedCallback) {
        this.stateChangedCallback = stateChangedCallback;
    }

    public class SettleRunnable implements Runnable {
        private final int mTargetState;
        private final View mView;

        SettleRunnable(View view, int i) {
            this.mView = view;
            this.mTargetState = i;
        }

        @Override
        public void run() {
            if (DragStopView.this.mViewDragHelper != null && DragStopView.this.mViewDragHelper.continueSettling(true)) {
                ViewCompat.postOnAnimation(this.mView, this);
            } else {
                DragStopView.this.setStateInternal(this.mTargetState);
            }
        }
    }

    public static class SavedState extends BaseSavedState {
        public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
            @Override
            public SavedState createFromParcel(Parcel source) {
                return new SavedState(source);
            }

            @Override
            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
        final int dragDist;
        final int state;

        private SavedState(Parcel parcel) {
            super(parcel);
            this.state = parcel.readInt();
            this.dragDist = parcel.readInt();
        }

        private SavedState(Parcelable parcelable, int i, int i2) {
            super(parcelable);
            this.state = i;
            this.dragDist = i2;
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            super.writeToParcel(parcel, i);
            parcel.writeInt(this.state);
            parcel.writeInt(this.dragDist);
        }
    }
}
